package san.web.article;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import san.entity.article.Article;
import san.entity.article.ArticleItem;
import san.service.article.ArticleService;
import com.utils.Page;
import com.web.struts2.CRUDSupportAction;

@Results( { @Result(name = "reload", location = "article.shtml", type = "redirect") })
public class ArticleAction extends CRUDSupportAction {
	/**
	 * 文章 action
	 */
	private static final long serialVersionUID = -4154867833465001683L;
	private Page page = new Page(10);// 每页10条记录
	@Autowired
	private ArticleService articleService;
	private Article article;
	private String search_text;
	List<ArticleItem> allArticleItem;

	@Override
	protected void prepareModel() throws Exception {
		if (article != null && StringUtils.isNotBlank(article.getId())) {
			article = articleService.getArticleById(article.getId());
		} else {
			if (StringUtils.isNotBlank(getId())) {
				article = articleService.getArticleById(getId());
			} else {
				article = new Article();
			}
		}
	}

	@Override
	public String delete() throws Exception {
		if (StringUtils.isBlank(getId())) {
			addActionError("删除失败!");
		} else {
			articleService.removeArticleType(getId());
			addActionMessage("删除成功!");
		}
		return list();
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		if (article == null) {
			article = new Article();
		}
		setPage(articleService.getPageForArticle(page));
		return SUCCESS;
	}

	@Override
	public String save() throws Exception {
		if (StringUtils.isBlank(article.getId())) {
			articleService.addArticle(article.getArticletype());
		} else {
			articleService.updateArticleType(article.getId(), article
					.getArticletype());
		}
		return "reload";
	}

	/*
	 * 全文检索
	 */
	@SuppressWarnings("finally")
	public String search() throws Exception {
		try {
			if (search_text != null && !search_text.trim().equals("")) {
				setAllArticleItem(articleService.getText(search_text));
			}
		} catch (Exception e) {
			addActionError("没有搜索到结果!");
		} finally {
			return "search";
		}
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public String getSearch_text() {
		return search_text;
	}

	public void setSearch_text(String search_text) {
		this.search_text = search_text;
	}

	public List<ArticleItem> getAllArticleItem() {
		return allArticleItem;
	}

	public void setAllArticleItem(List<ArticleItem> allArticleItem) {
		this.allArticleItem = allArticleItem;
	}

}
